//
//  CategoryViewController.m
//  KeepFit
//
//  Created by Fuzzie Liu on 15/10/28.
//  Copyright © 2015年 Fuzzie Liu. All rights reserved.
//

#import "CategoryViewController.h"
#import "HomePageCell.h"
#import "LeiModel.h"
#import "DetailViewController.h"
#import <MBProgressHUD.h>
#import "SouCangViewController.h"
#import "SecondViewController.h"
@interface CategoryViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, MBProgressHUDDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *leiArr;
@end

@implementation CategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"所有食材";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-sousuo@3x.png"] style:UIBarButtonItemStyleDone target:self action:@selector(barButtonSouSuoAction:)];
  
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"fff" style:UIBarButtonItemStyleDone target:self action:@selector(dianjiAction:)];
    
    [self handleData];
    self.leiArr = [NSMutableArray array];
  
    UICollectionViewFlowLayout *folwLayout = [[UICollectionViewFlowLayout alloc] init];
    folwLayout.itemSize = CGSizeMake((self.view.frame.size.width - 40) / 3, self.view.frame.size.height / 6 );
    folwLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    folwLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    folwLayout.minimumLineSpacing = 0;
    folwLayout.minimumInteritemSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:folwLayout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    UINib *nib = [UINib nibWithNibName:@"HomePageCell" bundle:[NSBundle mainBundle]];
    
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"HomePage"];
 
}
- (void)barButtonSouSuoAction:(UIBarButtonItem *)bnt
{
    SouCangViewController *souCangVC = [[SouCangViewController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:souCangVC];
    navi.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:navi animated:YES completion:^{
        
    }];
  
}
//- (void)dianjiAction:(UIBarButtonItem *)item
//{
//    SecondViewController *secondVC = [[SecondViewController alloc] init];
//    [self.navigationController pushViewController:secondVC animated:YES];
//    
//}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.leiArr.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomePageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomePage" forIndexPath:indexPath];
    LeiModel *model = [self.leiArr objectAtIndex:indexPath.row];
    [cell.leiImageView sd_setImageWithURL:[NSURL URLWithString:model.image_url] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    cell.nameLab.text = model.name;
//    cell.backgroundColor = [UIColor redColor];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LeiModel *model = [self.leiArr objectAtIndex:indexPath.row];
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    detailVC.title = model.name;
    detailVC.detailId = [model.leiId stringValue];
    [self.navigationController pushViewController:detailVC animated:YES];
    
}



- (void)handleData
{
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hub.color = [UIColor colorWithRed:0.13 green:0.10 blue:0.10 alpha:0.9];
    hub.removeFromSuperViewOnHide = YES;
    
    hub.delegate = self;
    hub.labelText = @"得之我幸,失之我命,如此安好...";
    
    NSString *string = @"http://food.boohee.com/fb/v1/categories?kind=group";
    
    [NetWorkingCache getUrl:string success:^(id result) {
        NSDictionary * bigDic = result;
        NSArray *bigArr = [bigDic objectForKey:@"categories"];
        for (NSDictionary *dic in bigArr) {
            LeiModel *leiModel = [LeiModel modelWithDic:dic];
            [self.leiArr addObject:leiModel];
        }
        
        [self.collectionView reloadData];
        [hub hide:YES];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
  
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
